package com.uchengwang.ui.cm_ui.me.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/5/5.
 */

public class LinkCycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<CycleBean> data = new ArrayList<>();

    public LinkCycleAdapter (Context context){
        this.context = context;
    }

    public void setData(List<CycleBean> data){
        this.data = data;
        notifyDataSetChanged();
    }

    public List<CycleBean> getData(){
        return data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_ex_main_child_view, parent, false);
        return new LinkVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        final LinkVH holder = (LinkVH) h;
        final CycleBean bean = data.get(position);
        ImageManager.getInstance().loadImage(context, holder.child_head, bean.getCycleCover(), ImageManager.ImageType.ROUND);

        holder.child_title.setText(bean.getCycleName());

        if (bean.isConn()){
            ImageManager.getInstance().loadImage(context, holder.child_follow, R.drawable.icon_follow_yes, ImageManager.ImageType.NARMOL);
        }else {
            ImageManager.getInstance().loadImage(context, holder.child_follow, R.drawable.icon_follow_normal, ImageManager.ImageType.NARMOL);
        }

        holder.child_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CycleHttpUtils.getUtils().clubLinkCycle((Activity) context, !bean.isConn(), bean.getId(), new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            bean.setConn(!bean.isConn());

                            if (bean.isConn()){
                                //原本是关联的 现在非关联
                                ImageManager.getInstance().loadImage(context, holder.child_follow, R.drawable.icon_follow_normal, ImageManager.ImageType.NARMOL);
                            }else {
                                ImageManager.getInstance().loadImage(context, holder.child_follow, R.drawable.icon_follow_yes, ImageManager.ImageType.NARMOL);

                            }

                        }else {
                            UCommonUtil.getUtil().showToast(context, msg);
                        }
                    }
                });
            }
        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class LinkVH extends RecyclerView.ViewHolder {
        private ImageView child_head, child_follow;
        private TextView child_title;
        private View itemView;

        public LinkVH(View view) {
            super(view);

            itemView = view;
            AutoUtils.auto(view);

            child_head = view.findViewById(R.id.child_head);
            child_follow = view.findViewById(R.id.child_follow);
            child_title = view.findViewById(R.id.child_title);
        }
    }
}
